/**
 * copyright (c) 2017-2023 厦门风领科技有限公司
 * https://www.fengling.cn/
 * 
 * zengbinsi
 * 2019-02-28
 */

 import path from 'path';
 import pathUtil from '../libs/utils/path-utils';
 import fileUtil from '../libs/utils/file-utils';
 const srcPluginDir = pathUtil.getPathInTemplateAssets(path.join('bundles', 'framework', 'plugin'));
 const dstPluginDir = pathUtil.getPathInAssets(path.join('bundles', 'framework', 'plugin'));
 
 
 
export const initFilePluginSocketIO = (callback?: Function) => {
    // 创建框架插件脚本目录
    pathUtil.mkdirsSyncInAssets('bundles/framework/plugin/net', async () => {
        let srcFile = path.join(srcPluginDir, 'Net', 'socket-io.js');
        let distFile = path.join(dstPluginDir, 'Net', 'socket-io.js');
        fileUtil.copy(srcFile, distFile, true);

        srcFile = path.join(srcPluginDir, 'Net', 'socket-io.js.meta');
        distFile = path.join(dstPluginDir, 'Net', 'socket-io.js.meta');
        fileUtil.copy(srcFile, distFile, true);
        console.log('[tool-box]Import socket-io.js successed!');
        await pathUtil.refreshAsset('db://assets/bundles/framework/plugin/net/socket-io.js');
        console.log('[tool-box]bundles/framework/plugin/net/socket-io.js refreshed!');
        callback && callback();
    });
};
 
export const initFilePluginCryptoJS = (callback?: Function) => {
    // 引入插件脚本
    pathUtil.mkdirsSyncInAssets('bundles/framework/plugin/crypto-js', async () => {
        let srcFile = path.join(srcPluginDir, 'crypto-js', 'aes.js');
        let distFile = path.join(dstPluginDir, 'crypto-js', 'aes.js');
        fileUtil.copy(srcFile, distFile, true);
        srcFile = path.join(srcPluginDir, 'crypto-js', 'aes.js.meta');
        distFile = path.join(dstPluginDir, 'crypto-js', 'aes.js.meta');
        fileUtil.copy(srcFile, distFile, true);

        srcFile = path.join(srcPluginDir, 'crypto-js', 'CryptoJS.js');
        distFile = path.join(dstPluginDir, 'crypto-js', 'CryptoJS.js');
        fileUtil.copy(srcFile, distFile, true);
        srcFile = path.join(srcPluginDir, 'crypto-js', 'CryptoJS.js.meta');
        distFile = path.join(dstPluginDir, 'crypto-js', 'CryptoJS.js.meta');
        fileUtil.copy(srcFile, distFile, true);

        srcFile = path.join(srcPluginDir, 'crypto-js', 'pad-zeropadding.js');
        distFile = path.join(dstPluginDir, 'crypto-js', 'pad-zeropadding.js');
        fileUtil.copy(srcFile, distFile, true);
        srcFile = path.join(srcPluginDir, 'crypto-js', 'pad-zeropadding.js.meta');
        distFile = path.join(dstPluginDir, 'crypto-js', 'pad-zeropadding.js.meta');
        fileUtil.copy(srcFile, distFile, true);

        console.log('[tool-box]Import crypto-js successed!');
        await pathUtil.refreshAsset('db://assets/bundles/framework/plugin/crypto-js');
        console.log('[tool-box]bundles/framework/plugin/crypto-js refreshed!');
        callback && callback();
    });
};
 
export const initFilePluginProtobuf = (callback?: Function) => {
    // 引入插件脚本
    pathUtil.mkdirsSyncInAssets('bundles/framework/plugin/protobuf', async () => {
        let srcFile = path.join(srcPluginDir, 'protobuf', 'bytebuffer.js');
        let distFile = path.join(dstPluginDir, 'protobuf', 'bytebuffer.js');
        fileUtil.copy(srcFile, distFile, true);
        srcFile = path.join(srcPluginDir, 'protobuf', 'bytebuffer.js.meta');
        distFile = path.join(dstPluginDir, 'protobuf', 'bytebuffer.js.meta');
        fileUtil.copy(srcFile, distFile, true);

        srcFile = path.join(srcPluginDir, 'protobuf', 'long.js');
        distFile = path.join(dstPluginDir, 'protobuf', 'long.js');
        fileUtil.copy(srcFile, distFile, true);
        srcFile = path.join(srcPluginDir, 'protobuf', 'long.js.meta');
        distFile = path.join(dstPluginDir, 'protobuf', 'long.js.meta');
        fileUtil.copy(srcFile, distFile, true);

        srcFile = path.join(srcPluginDir, 'protobuf', 'protobuf.js');
        distFile = path.join(dstPluginDir, 'protobuf', 'protobuf.js');
        fileUtil.copy(srcFile, distFile, true);
        srcFile = path.join(srcPluginDir, 'protobuf', 'protobuf.js.meta');
        distFile = path.join(dstPluginDir, 'protobuf', 'protobuf.js.meta');
        fileUtil.copy(srcFile, distFile, true);

        console.log('[tool-box]Import protobufJS successed!');
        pathUtil.refreshAsset('db://assets/bundles/framework/plugin/protobuf');
        console.log('[tool-box]bundles/framework/plugin/protobuf refreshed!');
        callback && callback();
    });
};
 
/** 微信jssd */
export const initFilePluginWechatH5JSSDK = (callback?: Function) => {
    // 创建框架插件脚本目录
    pathUtil.mkdirsSyncInAssets('bundles/framework/plugin/wx-h5-jssdk', async () => {
        let srcFile = path.join(srcPluginDir, 'wx-h5-jssdk', 'weixin-1.0.0.js');
        let distFile = path.join(dstPluginDir, 'wx-h5-jssdk', 'weixin-1.0.0.js');
        fileUtil.copy(srcFile, distFile, true);

        srcFile = path.join(srcPluginDir, 'wx-h5-jssdk', 'weixin-1.0.0.js.meta');
        distFile = path.join(dstPluginDir, 'wx-h5-jssdk', 'weixin-1.0.0.js.meta');
        fileUtil.copy(srcFile, distFile, true);
        console.log('[tool-box]Import weixin-1.0.0.js successed!');
        await pathUtil.refreshAsset('db://assets/bundles/framework/plugin/wx-h5-jssdk/weixin-1.0.0.js');
        console.log('[tool-box]bundles/framework/plugin/wx-h5-jssdk/weixin-1.0.0.js refreshed!');
        callback && callback();
    });
};